package com.qf.dao.impl;

import com.qf.dao.OrderDao;
import com.qf.domain.Order;
import com.qf.domain.OrderDetail;
import com.qf.utils.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    private QueryRunner queryRunner=new QueryRunner();
    //Connection connection= DataSourceUtil.getConnection();

    @Override
    public void addOrder(Order order) {
        Connection connection= DataSourceUtil.getConnection();
        String sql=" INSERT INTO `tb_order`(`id`,`uid`,`money`,`status`,`time`,`aid` ) VALUES(?,?,?,?,?,?) ";
        Object [] params={order.getId(),order.getUid(),order.getMoney(),order.getStatus(),order.getTime(),order.getAid()};
        try {
            queryRunner.update(connection,sql,params );
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DataSourceUtil.closeAll(null,null,connection);
        }
    }

    @Override
    public void addOrderDetail(OrderDetail orderDetail) {
        Connection connection= DataSourceUtil.getConnection();
        String sql=" INSERT INTO `tb_orderdetail`(`oid`,`pid`,`num`,`money`) VALUES (?,?,?,?); ";
        Object [] params={orderDetail.getOid(),orderDetail.getPid(),orderDetail.getNum(),orderDetail.getMoney()};
        try {
            queryRunner.update(connection,sql,params );
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DataSourceUtil.closeAll(null,null,connection);
        }
    }

    @Override
    public List<Order> findByUserId(Integer uid) {
        Connection connection= DataSourceUtil.getConnection();
        String sql=" SELECT `id`,`uid`,`money`,`status`,`time`,`aid` FROM `tb_order` WHERE `uid`=? ";
        try {
            return queryRunner.query(connection,sql,new BeanListHandler<>(Order.class),uid);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DataSourceUtil.closeAll(null,null,connection);
        }
    }

    @Override
    public Order findByOid(String oid) {
        Connection connection= DataSourceUtil.getConnection();
        String sql=" SELECT `id`,`uid`,`money`,`status`,`time`,`aid` FROM `tb_order` WHERE `id`=? ";
        try {
            return queryRunner.query(connection, sql,new BeanHandler<>(Order.class),oid);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DataSourceUtil.closeAll(null,null,connection);
        }
    }

    @Override
    public List<OrderDetail> findDetail(String oid) {
        Connection connection= DataSourceUtil.getConnection();
        String sql="SELECT `id`,`oid`,`pid`,`num`,`money` FROM `tb_orderdetail` WHERE `oid`=? ";
        try {
            return queryRunner.query(connection, sql,new BeanListHandler<>(OrderDetail.class),oid);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DataSourceUtil.closeAll(null,null,connection);
        }
    }

    @Override
    public List<Order> findAll(String where, List<Object> params) {
        Connection connection=DataSourceUtil.getConnection();
        String sql=" SELECT * FROM `tb_order` "+where ;
        try {
            return queryRunner.query(connection,sql,new BeanListHandler<>(Order.class),params.toArray());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DataSourceUtil.closeAll(null,null,connection);
        }
    }

    @Override
    public void removeByOId(String oid) {
        Connection connection=DataSourceUtil.getConnection();
        String sql1=" DELETE FROM `tb_orderdetail` WHERE `oid`=? ";
        String sql2=" DELETE FROM `tb_order` WHERE `id`=? ";
        try {
            queryRunner.update(connection,sql1,oid);
            queryRunner.update(connection,sql2,oid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtil.closeAll(null,null,connection);
        }
    }

    @Override
    public void modifyStatusById3(String oid) {
        Connection connection=DataSourceUtil.getConnection();
        String sql=" UPDATE `tb_order` SET `status`=3 WHERE `id`=? ";
        try {
            queryRunner.update(connection,sql,oid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtil.closeAll(null,null,connection);
        }
    }

    @Override
    public void modifyStatusById2(String oid) {
        Connection connection=DataSourceUtil.getConnection();
        String sql=" UPDATE `tb_order` SET `status`=2 WHERE `id`=? ";
        try {
            queryRunner.update(connection,sql,oid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtil.closeAll(null,null,connection);
        }
    }

    @Override
    public void modifyStatusById4(String oid) {
        Connection connection=DataSourceUtil.getConnection();
        String sql=" UPDATE `tb_order` SET `status`=4 WHERE `id`=? ";
        try {
            queryRunner.update(connection,sql,oid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtil.closeAll(null,null,connection);
        }
    }
}
